package domain.controller;

import java.io.IOException;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.data.QnAPersistenceManager;
import domain.model.Whois;

@SuppressWarnings("serial")
public class OwerServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.getWriter().println("Hello, world");
        String ip = req.getRemoteAddr();

        req.setAttribute("ip", ip);
        PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
        
        String path = ((HttpServletRequest)req).getRequestURI();
		StringTokenizer st = new StringTokenizer( path,"/");
        int count = st.countTokens(); 

        if(count != 2)
        {
        	resp.sendRedirect("/");
        }
        
		// skip one token /question/abcd (remove sites)
		st.nextToken();
		String title_url = st.nextToken();
        
		Query query = psm.newQuery(Whois.class);
		query.setFilter("aliasUser =='"+title_url+"'");
		query.setRange(0,100);
		@SuppressWarnings("unchecked")
		List<Whois> listEmails = (List<Whois>) query.execute();
		req.setAttribute("listEmails", listEmails);
		req.setAttribute("email", "Owner : "+title_url);
		
		req.setAttribute("title", "Owner - "+title_url);
		req.setAttribute("description", "Owner - "+title_url+"the website of the same Owner "+title_url+" is synthesized by domain-infomation.appspot.com");
		req.setAttribute("keyword", title_url+",linkout,Domain name information,  Server information, Web site analysis, website traffic details, Website backlinks website IP, website ranking info,topic");
		
		try {
			req.getRequestDispatcher("/ower.jsp").forward(req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

}
